<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Two Sum 动态演示（随机数组）</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    .container {
      max-width: 600px;
      margin: 0 auto;
    }
    .step {
      margin-bottom: 20px;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #f9f9f9;
    }
    .highlight {
      background-color: yellow;
      font-weight: bold;
    }
    .result {
      color: green;
      font-weight: bold;
    }
    button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Two Sum 动态演示（随机数组）</h1>
    <p>给定数组: <span id="nums"></span></p>
    <p>目标值: <span id="target"></span></p>
    <div id="steps"></div>
    <button onclick="startDemo()">开始演示</button>
  </div>

  <script>
    let nums = [];
    let target = 0;
    let stepIndex = 0;
    let numToIndex = {};

    // 生成随机数组和目标值
    function generateRandomArray() {
      const length = Math.floor(Math.random() * 10) + 5; // 数组长度 5-14
      nums = Array.from({ length }, () => Math.floor(Math.random() * 20) + 1); // 随机数 1-20
      const index1 = Math.floor(Math.random() * length);
      let index2;
      do {
        index2 = Math.floor(Math.random() * length);
      } while (index2 === index1); // 确保两个下标不同
      target = nums[index1] + nums[index2]; // 目标值为两个随机数的和
    }

    // 初始化显示
    function initialize() {
      generateRandomArray();
      document.getElementById("nums").textContent = JSON.stringify(nums);
      document.getElementById("target").textContent = target;
    }

    // 开始演示
    function startDemo() {
      stepIndex = 0;
      numToIndex = {};
      document.getElementById("steps").innerHTML = "";
      nextStep();
    }

    // 执行下一步
    function nextStep() {
      if (stepIndex >= nums.length) {
        addStep("遍历结束，未找到符合条件的两个数。");
        return;
      }

      const currentNum = nums[stepIndex];
      const complement = target - currentNum;
      const stepsDiv = document.getElementById("steps");

      // 显示当前步骤
      let stepText = `步骤 ${stepIndex + 1}: 当前数字 = <span class="highlight">${currentNum}</span>, 补数 = ${complement}`;
      if (complement in numToIndex) {
        stepText += `<br>找到补数 ${complement} 在哈希表中，下标为 ${numToIndex[complement]}。`;
        stepText += `<br><span class="result">结果: [${numToIndex[complement]}, ${stepIndex}]</span>`;
        addStep(stepText);
        return;
      } else {
        stepText += `<br>补数 ${complement} 不在哈希表中，将当前数字 ${currentNum} 存入哈希表。`;
        numToIndex[currentNum] = stepIndex;
        addStep(stepText);
      }

      stepIndex++;
      setTimeout(nextStep, 2000); // 2 秒后执行下一步
    }

    // 添加步骤到页面
    function addStep(html) {
      const stepDiv = document.createElement("div");
      stepDiv.className = "step";
      stepDiv.innerHTML = html;
      document.getElementById("steps").appendChild(stepDiv);
    }

    // 页面加载时初始化
    window.onload = initialize;
  </script>
</body>
</html>